//	@2023-09-26： 基于旧版本修改而来，增加了可以保存配置参数的结构体，为以后的功能扩展做好准备。

#include "SchmidtCAM.h"

int main( int argc, char *argv[] ){

	int fd = open("/dev/mem", O_RDWR | O_SYNC);
	uint *ptr1 = mmap(NULL, 10, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ADDR_GPIO_OUT);
	uint *gpio_reg 		= ptr1;		// 0x41200000
	uint *gpio2_reg		= ptr1+2;	// 0x41200008

	int enable_ad_clk 			= 0;
	int enable_ad_clk_opt_set 	= 0;

	char config_fname[128];
	int config_fname_set 	= 0;

	int opt;
	opterr = 1;
	while( (opt=getopt(argc,argv,"e:c:")) != -1 ){
		switch(opt){
			case 'e':
				enable_ad_clk 			= atoi(optarg);
				enable_ad_clk_opt_set 	= 1;
				printf("==> ad_clk clock enable opt: %d\n", enable_ad_clk);
				break;
			case 'c':
				sprintf(config_fname, "%s", optarg);
				config_fname_set 	= 1;
				printf("==> Configuring ad_clk clock from config file:\n\t%s\n", optarg);
				break;
			default:
				printf("other option:%c\n", opt);
		}
	}

	if( enable_ad_clk_opt_set == 1 ){
		if( enable_ad_clk == 0){
			clrbit(*gpio_reg, BIT_156MHz_EN);
		}
		else if( enable_ad_clk == 1 ){
			setbit(*gpio_reg, BIT_156MHz_EN);
		}
	}

	if( config_fname_set != 1 ) {
		printf("##> adc2reg_clk configuration file is not provided, quit ...\n");
		// return 0;
	} else {

		ADC2REG_CLK_CONFIG cfg;
		init_adc2reg_clk_config( &cfg );
		read_adc2reg_clk_config( &cfg, config_fname, "ADC2REG" );

		// printf("> debug ...\n");

		config_adc2reg_clk_via_SPI( gpio_reg, gpio2_reg, &cfg );
		// exit(0);

		free_adc2reg_clk_config( &cfg );
	}
	
	munmap(ptr1,10);
	close(fd);

	return 0;
}

